package com.hdjk.view.callback;

import com.hdjk.view.entity.Performance;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

public abstract class GetWalletHistoricalDataCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONArray obj = (JSONArray) msg.getData();
        if (obj != null) {
            if (obj.length() > 0) {
                List<Performance> list = new ArrayList<>();

                JSONArray array = obj.getJSONObject(0).getJSONArray("data");
                if (null == array || array.length() == 0) return msg;

                for (int j = 0; j < array.length(); j++) {
                    Performance performance = new Performance();
                    JSONObject object = array.getJSONObject(j);
                    if (!object.isNull("nav")) {
                        performance.setNav(object.getDouble("nav"));
                    }
                    if (!object.isNull("earningsPer10000")) {
                        performance.setEarningsPer10000(object.getDouble("earningsPer10000"));
                    }
                    if (!object.isNull("sevenDaysAnnualizedYield")) {
                        performance.setSevenDaysAnnualizedYield(object.getDouble("sevenDaysAnnualizedYield"));
                    }
                    if (!object.isNull("navDate")) {
                        performance.setNavDate(object.getString("navDate"));
                    }
                    list.add(performance);
                }
                msg.setData(list);
            }
        }
        return msg;
    }
}